﻿using System;
using System.Collections;
using System.Collections.Generic;
//using IBatisNet.DataAccess;
using Wsitech.Model;
using Wsitech.Utility;

namespace Wsitech.BLL
{
    /// <summary>
    /// 功能：     CompanyInfo业务逻辑实现
    /// 作者：     LI
    /// 日期：     5/16/2012
    /// </summary>
    public class CompanyInfoBLL
    {
        private Wsitech.Persistence.Interface.ICompanyInfoDao dao = null;

        private CompanyInfoBLL()
        {
            dao = Wsitech.Persistence.Dao.CompanyInfoSqlMapDao.Instance;
        }

        #region （懒汉）单例模式
        private static CompanyInfoBLL instance = null;
        private static object syncRoot = new object();
        public static CompanyInfoBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new CompanyInfoBLL();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion

        public static readonly string COMPANY_LEGEND_ID = "11";


        #region	基本功能 不能修改


        /// <summary>
        /// 根据主键得到明细
        /// </summary>
        /// <returns></returns>
        public CompanyInfo GetDetail(System.String companyid)
        {
            try
            {
                return dao.GetDetail(companyid);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public IList<CompanyInfo> GetList()
        {
            try
            {
                IList<CompanyInfo> list = dao.GetList();
                return list;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private IList<CompanyInfo> GetListByParams(object TopCounts, CompanyInfo companyinfo)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                IList<CompanyInfo> list = dao.GetListByParams(ht);
                return list;
            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="companyinfo"></param>
        /// <returns></returns>
        public object Add(CompanyInfo companyinfo)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Add, GetDetail(companyinfo.CompanyId));
                return dao.Add(companyinfo);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="companyinfo"></param>
        /// <returns></returns>
        public int Update(CompanyInfo companyinfo)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Update, GetDetail(companyinfo.CompanyId));
                return dao.Update(companyinfo);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="CompanyId"></param>
        /// <returns></returns>
        public int Delete(System.String companyid)
        {
            try
            {
                return dao.Delete(companyid);
            }
            catch
            {
                throw;
            }
        }

        #endregion


        #region 扩展功能

        #endregion

        #region 根据ID获取公司名称
        /// <summary>
        /// 获取公司名称
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public string GetCompanyName(string companyId)
        {
            CompanyInfo company = dao.GetDetail(companyId);
            if (company != null)
            {
                return company.CompanyName;
            }
            else
            {
                return "找不到公司信息";
            }
        }
        public string GetEnCompanyName(string companyId)
        {
            CompanyInfo company = dao.GetDetail(companyId);
            if (company != null)
            {
                return company.EnCompanyName;
            }
            else
            {
                return "Company Not Found";
            }
        }
        #endregion

        /// <summary>
        /// 获取可用列表
        /// </summary>
        /// <returns></returns>
        public IList<CompanyInfo> GetActiveList()
        {
            Hashtable ht = new Hashtable();
            ht.Add("state", (int)UtilityEnum.State.Enable);
            return dao.GetListByParams(ht);
        }

        #region 添加新成员企业

        /// <summary>
        ///  添加新的成员企业 
        /// </summary>
        /// <param name="companyName"></param>
        /// <param name="fullName"></param>
        /// <param name="description"></param>
        /// <param name="indexUrl"></param>
        /// <param name="jobUrl"></param>
        public void AddCompany(string companyName, string fullName, string enName, string description, string indexUrl, string jobUrl)
        {
            CompanyInfo info = new CompanyInfo();
            info.CompanyId = Guid.NewGuid().ToString();
            info.CompanyName = companyName;
            info.EnCompanyName = enName;
            info.FullName = fullName;
            info.Description = description;
            info.IndexUrl = indexUrl;
            info.JobUrl = jobUrl;
            info.JobUrlEnabled = false;

            info.State = (int)UtilityEnum.State.Enable;
            info.CreateTime = DateTime.Now;
            info.UpdateTime = DateTime.Now;

            Add(info);
        }

        #endregion


    }
}
